package com.google.code.whitetask;

import static com.google.code.whitetask.Result.SUCCESS;

public class ConditionalTask implements Task {
  private Result expectedResult;
  private Task task;
  
  public ConditionalTask(Result result, Task task) {
    this.expectedResult = result;
    this.task = task;
  }

  public Result execute(Context context) {
    //TODO Duplicated code 
    //TODO Following inspection code can be moved to the syntax inspector rather than here.
    if (context.isThereLastTaskBeingRan() == false) {
      throw new IllegalStateException(
          "If you are using the notation as 'ifLastResult...' on DSL,"
              + "you should check whether there is a previou task " + ""
              + "which is executed prior to using the notation.");
    }
    
    if (context.getLastResult() != expectedResult) {
      return SUCCESS;
    }
    
    return task.execute(context);
  }
}
